/**
 * Created by 万林 on 2016/10/22 0022.
 */


var aIframe = null;
var headTabbarArray=null;
var headTabbarTitleArray=null;
var player = null;
var playImage = null;
var progresRang = null;
var playButton = null;
var stopButton = null;

window.onload=function () {
    initUserInterFace();
};

window.onscroll = function () {
    var headTabBarTitleMainDiv =null;
    var topNumber =  200-window.pageYOffset;
    headTabBarTitleMainDiv = document.getElementById("headTabBarTitleMainDiv");
    // if (topNumber<=0&&window.TabbarPositionStyle != "fixed"){
    //     AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:0});
    //     window.TabbarPositionStyle = "fixed";
    // }else if (topNumber>0){
    //     AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:topNumber-80});
    //     window.TabbarPositionStyle = "absolute";
    // }

    if (topNumber<=0&&window.TabbarPositionStyle != "fixed"){
       scroToTop(false);
    }else if (topNumber>0&&window.TabbarPositionStyle != "absolute"){
       scroToTitle(false);
    }

    function scroToTitle(flag) {
        headTabBarTitleMainDiv = document.getElementById("headTabBarTitleMainDiv");
        AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:topNumber-80},function () {
            if ((200-window.pageYOffset)>=0&&(window.TabbarPositionStyle != "absolute"||flag)){
                headTabBarTitleMainDiv.style.top="120px";
                headTabBarTitleMainDiv.style.position = "absolute";
                window.TabbarPositionStyle = "absolute";
            }else {
                AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:-55});
            }
        });
    }
    function scroToTop(flag) {
        headTabBarTitleMainDiv = document.getElementById("headTabBarTitleMainDiv");
        headTabBarTitleMainDiv.style.top = window.pageYOffset-80+"px";
        AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:window.pageYOffset-55},function () {
            if ( (200-window.pageYOffset)<=0&&(window.TabbarPositionStyle != "fixed"||flag)){
                headTabBarTitleMainDiv.style.top="-55px";
                headTabBarTitleMainDiv.style.position = "fixed";
                window.TabbarPositionStyle = "fixed";
            }else {
                AnimatedObjEasyinout(headTabBarTitleMainDiv,{top:120});
            }
        });
    }
};

function initUserInterFace() {
    playButton = document.getElementById("playerButton");
    stopButton = document.getElementById("stopButton");
    progresRang = document.getElementById("progresRang");
    playImage = document.getElementById("playerImage");
    player = document.getElementById("player");
    aIframe = document.getElementById("contentIFrame");
    aIframe.onload = resizeIframe;

    aIframe.src="home.html";
    var headTabBarTitleDiv = document.getElementById("headTabBarTitleDiv");
    headTabbarArray =  getElementsByClassName(headTabBarTitleDiv,"headTabBarItem");
    headTabbarTitleArray =  getElementsByClassName(headTabBarTitleDiv,"headTabBarTitle");
    headTabbarArray[0].style.backgroundColor="#46C0DC";
    headTabbarTitleArray[0].style.borderBottom = "solid 1px white";

    progresRang.style.width=document.getElementById("controlsDiv").offsetWidth-120-60+"px";

    player.addEventListener("ended",function () {
        playButton.src = "../image/desk_play.png";
    },false);

    playButton.onclick = function () {
       playMusic();
    };
    stopButton.onclick = function () {
        player.currentTime = 0;
        player.pause();
        progresRang.value = 0;
        playButton.src = "../image/desk_play.png";
        document.getElementById("timeTextDiv").innerHTML ="00:00";
    };
    progresRang.addEventListener('touchstart',function () {
        progresRang.touched = true;
    },false);
    progresRang.addEventListener('touchend',function () {
        progresRang.touched = false;
    },false);
    progresRang.addEventListener('touchmove',function () {
        if (progresRang.touched){
            var minutes =  parseInt(progresRang.value/60);
            var seconds = parseInt(progresRang.value-minutes*60);
            document.getElementById("timeTextDiv").innerHTML =pad(minutes,2)+":"+pad(seconds,2);
        }
    },false);
    playImage.deg = 0;
}

function resizeIframe() {
    setTimeout(function () {
        AnimatedObjEasyinout(aIframe,{height: aIframe.contentWindow.document.documentElement.scrollHeight});
    },500);
    
}

function tabBarClick(obj,index) {
    var srcString = "";
    switch (index){
        case 0:
            aIframe.src="home.html";
            srcString = "../image/lxhHome.gif";
            break;
        case 1:
            aIframe.src="codeLife.html";
            srcString = "../image/lxhCodeLife.gif";
            break;
        case 2:
            aIframe.src="movie.html";
            srcString = "../image/lxhMovie.gif";
            break;
        case 3:
            aIframe.src="music.html";
            srcString = "../image/lxhMusic.gif";
            break;
        case 4:
            aIframe.src="anime.html";
            srcString = "../image/lxhAnime.gif";
            break;
        case 5:
            aIframe.src="about.html";
            srcString = "../image/lxhAbout.gif";
            break;
        default:
            aIframe.src="home.html";
            srcString = "../image/lxhHome.gif";
            break;
    }


    AnimatedObjEasyinout(BSKObj('headImage2'),{width:10,height:10,marginTop:60,marginLeft:50},function () {
        BSKObj('headImage2').src = srcString;
        AnimatedObjEasyinout(BSKObj('headImage2'),{width:80,height:80,marginTop:20,marginLeft:10})
    });
    
    for(var i = 0;i<headTabbarArray.length;i++){
        if(i==index){
            headTabbarArray[i].style.backgroundColor="#46C0DC";
        }else {
            headTabbarArray[i].style.backgroundColor = "#3F7B7F";
        }
    }
    for(var a=0;a<headTabbarTitleArray.length;a++){
        if (a==index){
            headTabbarTitleArray[a].style.borderBottom = "solid 1px white";
        }else {
            headTabbarTitleArray[a].style.borderBottom = "";
        }
    }
    scrollToTop();
    console.log(obj.innerHTML+"::"+window.pageYOffset+"‘’"+window.scrollY);
}

function scrollToTop() {

    var pageoffsetTimer = setInterval(function () {
        var bStop = true;
        var curentValue = Math.round(window.pageYOffset);
        var speed = (0-curentValue)/6;
        speed = speed>0?Math.ceil(speed):Math.floor(speed);
        if (curentValue!=0){
            bStop = false;
        }
        window.scrollTo(0,(curentValue+speed));
        if (bStop){
            clearInterval(pageoffsetTimer);
        }
    },30);
}

//打开播放面板
function openMusicPlayer(obj) {
    if(obj.opend){
        AnimatedObjEasyinout(document.getElementById("controlsDiv"),{left:document.getElementById("controlsDiv").offsetWidth*(-1)});
        AnimatedObjEasyinout(obj,{marginLeft:-30,width:60,height:60});
        obj.opend=false;
    }else {
        AnimatedObjEasyinout(document.getElementById("controlsDiv"),{left:0});
        AnimatedObjEasyinout(obj,{marginLeft:25,width:100,height:100});
        obj.opend=true;
    }

}
//播放器控制
var musicplayerTimer = null;
function playMusic() {

    if (player.paused){
        player.play();
        playButton.src = "../image/desk_pause.png";
        musicplayerTimer = setInterval(checkPlayerTime,30);
    }else {
        player.pause();
        clearInterval(musicplayerTimer);
        playButton.src = "../image/desk_play.png";
    }
}
function playerRangeChange() {
    player.currentTime = progresRang.value;
}

function checkPlayerTime() {
    if (!player.paused&&!progresRang.touched){
        progresRang.max=player.duration;
        progresRang.value = player.currentTime;
        var minutes =  parseInt(player.currentTime/60);
        var seconds = parseInt(player.currentTime-minutes*60);
        document.getElementById("timeTextDiv").innerHTML =pad(minutes,2)+":"+pad(seconds,2);
        rotate(playImage,playImage.deg+2);
        playImage.deg+=2;
        if (playImage.deg>=360){
            playImage.deg =0;
        }
    }
}

